/**
 * Created by ltx on 2017/10/5.
 */
(function () {
    window.onscroll=function(){
        var oDiv=document.getElementById('sendContact');
        var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
        // oDiv.style.top=document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop+'px';
        startMoveDiv(parseInt((document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop));
    }

    var timer=null;
    function startMoveDiv(iTarget){
        var oDiv=document.getElementById("sendContact");

        clearInterval(timer);
        timer=setInterval(function(){
            var speed=(iTarget-oDiv.offsetTop)/6;
            speed=speed>0?Math.ceil(speed):Math.floor(speed);
            if(oDiv.offsetTop==iTarget){
                clearInterval(timer);
            }
            else{
                oDiv.style.top=oDiv.offsetTop+speed+'px';
            }
        },30)
    }

    //markdown转过来
    $.ajax({
        type: 'GET',
        url: "/aboutMe",
        success: function (text) {
            //console.log(text);
            compile(text);
        }
    });

    //点击关闭加好友的那个框
     var $xxx=$('.xxx');
     var $sendContact=$('#sendContact');

     $xxx.on('click',function () {
         $sendContact.hide(500);
         $xxx.hide(500);
     })

    //发送添加的好友信息
    var $sedInfo = $('#sedInfo');
    $sedInfo.find('button').on('click', function() {
        //通过ajax提交请求
        $.ajax({
            type: 'post',
            url: '/user/contact',
            data: {
                username: $sedInfo.find('[name="username"]').val(),
                phone: $sedInfo.find('[name="phone"]').val(),
                email:  $sedInfo.find('[name="email"]').val()
            },
            dataType: 'json',
            success: function(result) {
                $sedInfo.find('.colWarning').html(result.message);
                if (!result.code) {
                    //注册成功
                    setTimeout(function() {
                       // $loginBox.show();
                        $sedInfo.hide();
                    }, 1000);
                }
            }
        });
    });
})()

function compile(text){
    var text = text[0].content;
    var converter = new showdown.Converter();
    var html = converter.makeHtml(text);
    document.getElementById("marked").innerHTML = html;
}